package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"medical/models"
)

type MedicalController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *MedicalController) Get() {

}

func (c *MedicalController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	aff := c.GetString("aff")
	tel := c.GetString("tel")
	num, _ := c.GetInt("num")
	medical := models.Medical{
		Name: name,
		Sex:  sex,
		Aff:  aff,
		Tel:  tel,
		Num:  num,
	}
	err := models.DB.Create(&medical).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *MedicalController) List() {
	var medical []models.Medical
	err := models.DB.Find(&medical).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.ServeJSON()
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功",
			Data: medical,
		}
		c.ServeJSON()
	}
}
